import { getZone, updateZone } from '../../../api/user'
import { findColumnList } from '../../../api/base'

Page({
  data: {
    list: [],
    personalInfo: {}
  },
  onLoad: function (options) {
    this.init()
  },

  init() {
    getZone().then(res => {
      this.setData({
        list: res.honour,
        personalInfo: res
      })
    })
  },

  editContent(e) {
    console.log(e)
    let list = this.data.list
    list[e.currentTarget.dataset.index] = e.detail.value
    this.setData({
      list: list
    })
  },

  clearHonour(e) {
    let list = this.data.list;
    list.splice(e.currentTarget.dataset.index, 1)
    this.setData({ list: list })
  },

  addHonour() {
    let list = this.data.list
    list.push('')
    this.setData({
      list: list
    })
  },

  done() {
    let personalInfo = this.data.personalInfo
    personalInfo.honour = this.data.list
    this.setData({
      personalInfo: personalInfo
    })
    const pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    updateZone(this.data.personalInfo).then(res => {
      wx.navigateBack({
        delta: 1,
        success(res) {
          prevPage.init()
        }
      })
    })
  }
})
